package me.atbar.moonlib.update;

import org.bukkit.plugin.Plugin;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class UpdateCheckAPI {

    private final Plugin plugin;
    private final String url;

    public UpdateCheckAPI(Plugin plugin, String url) {
        this.plugin = plugin;
        this.url = url;
    }

    public String getLatestVersion() {
        String version;
        try {
            URL url = new URL(this.url);
            InputStream inputStream = url.openStream();
            BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            version = bufferedInputStream.readLine();
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean isLatestVersion() {
        return plugin.getDescription().getVersion().equalsIgnoreCase(getLatestVersion());
    }

}
